package cn.wit.summit.chwan.widgets;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.ui.activity.MainActivity;
import cn.wit.summit.chwan.ui.base.MyApplication;
import cn.wit.summit.chwan.utils.AccountBean;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.MobileCodeUtils;
import cn.wit.summit.chwan.utils.PreferenceUtil;

/**
 * Created by tangbl on 2018/4/20.
 */

public class MessageMoneyDialog extends AlertDialog implements View.OnClickListener{

    private TextView dialogMessageTitleTx;
    private ImageView dialogMessageTagIv;
    private TextView dialogMessageTagTx;
    private TextView dialogMessageContentTx;
    private ImageView cancelIv;
    private Button dialogMessageCancelBt;
    private Button dialogMessageOkBt;

    public MessageMoneyDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCancelable(false);  //是否获取界外焦点
        Window dialogWindow = getWindow();
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.CENTER);
        setContentView(R.layout.dialog_message_money_view);
        dialogMessageTitleTx = findViewById(R.id.dialogMessageTitleTx);
        dialogMessageTagIv = findViewById(R.id.dialogMessageTagIv);
        dialogMessageTagTx = findViewById(R.id.dialogMessageTagTx);
        dialogMessageContentTx = findViewById(R.id.dialogMessageContentTx);

        dialogMessageCancelBt = findViewById(R.id.dialogMessageCancelBt);
        dialogMessageOkBt = findViewById(R.id.dialogMessageOkBt);
        cancelIv = findViewById(R.id.cancelIv);

        dialogMessageCancelBt.setOnClickListener(this);
        dialogMessageOkBt.setOnClickListener(this);
        cancelIv.setOnClickListener(this);
    }

    public void setShowType(int typeId) {
        String decStr = "";
        String money = "";
        AccountBean mAccountBean = AccountUtil.getInstance().getAccountData();
        if (mAccountBean==null) {
            money = "获得现金 0.5元";
        }
        switch (typeId) {
            case 1://分享
                if (mAccountBean!=null) {
                    money = "获得现金 "+mAccountBean.getShareMoney()+"元";
                }
                decStr = "每日首次分享";
                PreferenceUtil.saveLongPreference(getContext(), PreferenceUtil.PRE_FIRST_SHARE, System.currentTimeMillis());
                if (mAccountBean!=null) {
                    mAccountBean.setShareMoney(0);
                    MobileCodeUtils.taskShowDialogTag(getContext(), 3);
                }
                break;
            case 2://下载
                if (mAccountBean!=null) {
                    money = "获得现金 "+mAccountBean.getDownMoney()+"元";
                }
                decStr = "每日下载首款游戏";
                PreferenceUtil.saveLongPreference(getContext(), PreferenceUtil.PRE_FIRST_DOWNLOAD, System.currentTimeMillis());
                if (mAccountBean!=null) {
                    mAccountBean.setDownMoney(0);
                    MobileCodeUtils.taskShowDialogTag(getContext(), 2);
                }
                break;
            case 3://启动app
                if (mAccountBean!=null) {
                    money = "获得现金 "+mAccountBean.getRunAppMoney()+"元";
                }
                decStr = "每日首次启动"+getContext().getResources().getString(R.string.app_name);
                PreferenceUtil.saveLongPreference(getContext(), PreferenceUtil.PRE_FIRST_START_APP, System.currentTimeMillis());
                if (mAccountBean!=null) {
                    mAccountBean.setRunAppMoney(0);
                    MobileCodeUtils.taskShowDialogTag(getContext(), 1);
                }
                break;
        }
        dialogMessageTagIv.setImageResource(R.mipmap.ic_dialog_crash);
        dialogMessageTagTx.setText(money);
        dialogMessageContentTx.setText(decStr);
    }

    public void setCancelBtn(String txt, View.OnClickListener clickListener) {
        dialogMessageCancelBt.setOnClickListener(clickListener);
        dialogMessageCancelBt.setText(txt);
    }

    public void setOkBtn(String txt, View.OnClickListener clickListener) {
        dialogMessageOkBt.setOnClickListener(clickListener);
        dialogMessageOkBt.setText(txt);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialogMessageCancelBt:
                dismiss();
                Intent intent = new Intent();
                intent.setClass(getContext(), MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                Bundle bundle = new Bundle();
                bundle.putInt("MainPos", 2);
                intent.putExtras(bundle);
                getContext().startActivity(intent);
                break;
            case R.id.dialogMessageOkBt:
                dismiss();
                if (AccountUtil.getInstance().getUidNotToLogin(getContext()) == 0) return;
                IntentUtil.getInstance().goShareWebActivity(getContext(), MyApplication.cfgPlug.getValue_wallet_url_v2());
                break;
            case R.id.cancelIv:
                dismiss();
                break;
        }
    }
}
